<template>
    <div class="order-dialog">
        <el-dialog
            v-model="dialogFormVisible"
            title="房间预定"
            @close="close"
            :open="dialogOpen"
            :width="dialogWidth"
        >
            <el-form :model="form" ref="ruleForm" :rules="ruleForm">
                <el-form-item
                    label="日期"
                    :label-width="labelWidth"
                    prop="order_date"
                >
                    <el-date-picker
                        @change="dateChange"
                        value-format="YYYY-MM-DD"
                        v-model="form.order_date"
                        type="date"
                        placeholder="Pick a day"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item
                    label="场地"
                    :label-width="labelWidth"
                    prop="rooms"
                >
                    <el-select
                        v-model="form.rooms"
                        multiple
                        placeholder="Select"
                        clearable
                    >
                        <el-option-group
                            v-for="group in roomList"
                            :key="group.label"
                            :label="group.name"
                        >
                            <el-option
                                v-for="item in group.rooms"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            >
                            </el-option>
                        </el-option-group>
                    </el-select>
                </el-form-item>

                <el-form-item
                    label="时间段"
                    :label-width="labelWidth"
                    prop="time_slot"
                >
                    <el-select v-model="form.time_slot" placeholder="Select">
                        <el-option label="午市" value="午市"></el-option>
                        <el-option label="晚市" value="晚市"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="下午茶" :label-width="labelWidth">
                    <el-input v-model="form.tea" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item
                    :label-width="labelWidth"
                    prop="phone"
                >
                    <template #label>
                        <el-tooltip
                            class="item"
                            effect="dark"
                            content="如果没有联系方式请输入'-'"
                            placement="top-start"
                        >
                        <span>联系方式</span>
                        </el-tooltip>
                    </template>
                    <el-input
                        v-model="form.phone"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item
                    label="姓名"
                    :label-width="labelWidth"
                    prop="name"
                >
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="预订人数" :label-width="labelWidth">
                    <el-input
                        v-model="form.participants"
                        autocomplete="off"
                        type="number"
                    ></el-input>
                </el-form-item>
                <el-form-item label="餐标" :label-width="labelWidth">
                    <el-input
                        v-model="form.avg_cost"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="接单人" :label-width="labelWidth">
                    <el-input
                        v-model="form.book_by"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="备注" :label-width="labelWidth">
                    <el-input
                        v-model="form.comment"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="总结" :label-width="labelWidth">
                    <el-input
                        v-model="form.conclusion"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="close">取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')"
                        >保存</el-button
                    >
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { reactive, ref, toRef, watch } from "vue";
import { Inertia } from "@inertiajs/inertia";

export default {
    name: "CreateOrder",
    props: {
        dialogFormVisible: Boolean,
        date: String,
        roomList: Array,
        orderDate: Object || null
    },
    watch: {
        date(newVal) {
            for (const key in this.form) {
                if (key === "rooms") {
                    this.form[key] = [];
                } else {
                    this.form[key] = null;
                }
                this.form.order_date = newVal;
            }
        },
        orderDate:{
            deep: true,
            handler (newVal) {
                console.log(newVal);
                if(newVal){
                    for(let key in this.form){
                        this.form[key] = newVal[key]
                    }
                }
            }
        },
        screenWidth: {
            immediate: true,
            handler(val) {
                if (val > 1000) {
                    this.dialogWidth = "800px";
                } else {
                    this.dialogWidth = val - 50 + "px";
                }
            },
        },
    },
    data() {
        return {
            labelWidth: "100px",
            dialogWidth: "800px",
            screenWidth: "",
            form: {
                id: null,
                order_date: null,
                rooms: [],
                time_slot: null,
                tea: null,
                customer_id: null,
                participants: null,
                avg_cost: null,
                book_by: null,
                comment: null,
                name: null,
                phone: '-',
                conclusion: null,
            },
            ruleForm: {
                order_date: [
                    {
                        required: true,
                        message: "时间不能为空",
                        trigger: "blur",
                    },
                ],
                rooms: [
                    {
                        required: true,
                        message: "场地不能为空",
                        trigger: "change",
                    },
                ],
                time_slot: [
                    {
                        required: true,
                        message: "时间段不能为空",
                        trigger: "change",
                    },
                ],
                name: [
                    {
                        required: true,
                        message: "姓名不能为空",
                        trigger: "blur",
                    },
                ],
                phone: [
                    {
                        required: true,
                        message: "联系方式不能为空,如果没有请填写 '-'",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    mounted() {
        console.log(this.roomList)
        const that = this;
        that.screenWidth = document.body.clientWidth;
        window.onresize = () => {
            return (() => {
                that.screenWidth = document.body.clientWidth;
            })();
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //   alert("submit!");
                    this.form.origin = window.location.pathname.split("/")[1];
                    Inertia.post("/orders", this.form);
                    this.$emit("closeDialog");
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        close() {
            this.$emit("closeDialog");
            this.$refs["ruleForm"].resetFields();
        },
        dateChange(date) {
            console.log(date);
        },
    },
};
</script>

<style>
.el-dialog .el-input,
.el-dialog .el-select {
    width: 100% !important;
}
</style>
